﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using iCollect.CUPDataDataSetTableAdapters;
using iCollect.Constants;
using iCollect.CustomComponent;
using iCollect.util;

namespace iCollect.CollectDetail
{
    public partial class Channelconfig : UserControl
    {
        private BaseDeviceCeDian ds = null;
        public Channelconfig()
        {
            InitializeComponent();

        }

        private void tabInit()
        {
            ds.VTState();
        }

        private void tabChanged(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex == 0)
            {
                ds.VTState();
            }
            else if (tabControl1.SelectedIndex == 1)
            {
                ds.TEState();
            }
            else if (tabControl1.SelectedIndex == 2)
            {
                ds.PTState();
            }
            else if (tabControl1.SelectedIndex == 3)
            {
                ds.FITState();
            }
            else if (tabControl1.SelectedIndex == 4)
            {
                ds.RTState();
            }
        }

        private void channelChanged(object sender, EventArgs e)
        {
            string channelID = ((Button)sender).Text;
            string channelType = channelID.Substring(0, 1);
            switch (channelType)
            {
                case "V":
                case "Z":
                    tbVTID.Text = channelID;
                    loadVT(channelID);
                    break;
                case "T":
                    tbTEID.Text = channelID;
                    loadTE(channelID);
                    break;
                case "P":
                    tbPTID.Text = channelID;
                    loadPT(channelID);
                    break;
                case "F":
                    tbFTID.Text = channelID;
                    loadFT(channelID);
                    break;
                //转速
                case "R":
                    tbRTID.Text = channelID;
                    loadRT(channelID);
                    break;
                default:
                    
                    break;
            }
            if (channelType == "F")
            {
                String unit = "Kg/h";
                if (channelID.Equals("FT3649"))
                {
                    unit = "mmH2O";
                }
                else if (channelID.Equals("FT3653"))
                {
                    unit = "G/HR";
                }
                ftStopUnitLabel.Text = unit;
                ftAlertUnitLabel.Text = unit;
            }
        }

        private void targetChanged(object sender, EventArgs e)
        {
            int index = comboBoxTarget.SelectedIndex;
            string label;
            if ( index == 0)
            {
                label = "µm";
            }
            else if (index == 1)
            {
                label = "mm/s";
            }
            else
            {
                label = "m/s2";
            }
            vtAlertUnitLabel.Text = label;
            vtStopUnitLabel.Text = label;
            
        }

        private void label9_Click(object sender, EventArgs e)
        {

        }

        public void loadDevice()
        {
            loadSample();
            loadTabs();
            tabInit();
            resetFT();
            resetPT();
            resetTE();
            resetVT();
            resetRT();
        }


        private void loadTabs()
        {
            if (GlobalConstants.type == "0")
            {
                if (this.tabControl1.TabPages["tabPageTE"] == null)
                {
                    this.tabControl1.TabPages.Add(tabPageTE);
                }
                if (this.tabControl1.TabPages["tabPagePT"] == null)
                {
                    this.tabControl1.TabPages.Add(tabPagePT);
                }
                if (this.tabControl1.TabPages["tabPageFT"] == null)
                {
                    this.tabControl1.TabPages.Add(tabPageFT);
                }
                if (this.tabControl1.TabPages["tabPageRT"] == null)
                {
                    this.tabControl1.TabPages.Add(tabPageRT);
                }
            }
            else
            {
                if (this.tabControl1.TabPages["tabPageTE"] != null)
                {
                    this.tabControl1.TabPages.Remove(tabPageTE);
                }
                if (this.tabControl1.TabPages["tabPagePT"] != null)
                {
                    this.tabControl1.TabPages.Remove(tabPagePT);
                }
                if (this.tabControl1.TabPages["tabPageFT"] != null)
                {
                    this.tabControl1.TabPages.Remove(tabPageFT);
                }
                if (this.tabControl1.TabPages["tabPageRT"] != null)
                {
                    this.tabControl1.TabPages.Remove(tabPageRT);
                }
            }
            tabControl1.SelectedIndex = 0;
        }


        private void loadSample()
        {
            //this.panel2.Controls.Remove(ds);
            //if (GlobalConstants.type == "0")
            //{
            //    ds = new DeviceSample();

            //}
            //else if (GlobalConstants.type == "1")
            //{
            //    ds = new Device2Sample();
            //}
            //ds.Dock = System.Windows.Forms.DockStyle.Fill;
            //this.panel2.Controls.Add(ds);

            //ds.RaiseCustomEvent += channelChanged;
            ds = UIUtil.getInstance().loadSample(ds, panel2, channelChanged);
            //ds.RaiseCustomEvent += channelChanged;
        }

        /*
         * VT
         *
        */


        private void insertVT(string id)
        {
            try
            {
                channelvtTableAdapter.InsertQuery(id, GlobalConstants.currentDevice, 10, 4, "", 
                    0, 0, 0, 0, "");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }


        private void loadVT(string id)
        {
            try
            {
                string deviceCode = GlobalConstants.currentDevice;
                dtvt = channelvtTableAdapter.GetDataBy(deviceCode, id);
                if (dtvt.Rows.Count == 0)
                {
                    insertVT(id);
                    setDefaultVT();
                }
                else
                {
                    DataRow row = dtvt.Rows[0];
                    loadDataToControls(row, numVTAlert1, numVTAlert2,
                        numVTStop1, numVTStop2, tbTE);
                    cbVTLength.Text = row["length"].ToString();
                    cbVTFrequency.Text = row["frequency"].ToString();
                    comboBoxTarget.Text = row["type"].ToString();
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }

        private void resetVT()
        {
            tbVTID.Text = "";
            numVTAlert1.Value = 0;
            numVTStop1.Value = 0;
            numVTAlert2.Value = 0;
            numVTStop2.Value = 0;
            tbTE.Text = "";
            cbVTLength.Text = "10";
            cbVTFrequency.Text = "4";
            comboBoxTarget.Text = "";
        }

        private void setDefaultVT()
        {
            numVTAlert1.Value = 0;
            numVTStop1.Value = 0;
            numVTAlert2.Value = 0;
            numVTStop2.Value = 0;
            tbTE.Text = "";
            cbVTLength.Text = "10";
            cbVTFrequency.Text = "4";
            comboBoxTarget.Text = "";
        }


        /*
         * TE
         *
        */


        private void insertTE(string id)
        {
            try
            {
                channelteTableAdapter.InsertQuery(id, GlobalConstants.currentDevice, 0, 0, 0, 0, "");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }


        private void loadTE(string id)
        {
            try
            {
                string deviceCode = GlobalConstants.currentDevice;
                dtte = channelteTableAdapter.GetDataBy(deviceCode, id);
                if (dtte.Rows.Count == 0)
                {
                    insertTE(id);
                    setDefaultTE();
                }
                else
                {
                    DataRow row = dtte.Rows[0];
                    loadDataToControls(row, numTEAlert1, numTEAlert2,
                        numTEStop1, numTEStop2, tbTEComment);
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }

        private void resetTE()
        {
            tbTEID.Text = "";
            numTEAlert1.Value = 0;
            numTEAlert2.Value = 0;
            numTEStop1.Value = 0;
            numTEStop2.Value = 0;
            tbTEComment.Text = "";
        }

        private void setDefaultTE()
        {
            numTEAlert1.Value = 0;
            numTEAlert2.Value = 0;
            numTEStop1.Value = 0;
            numTEStop2.Value = 0;
            tbTEComment.Text = "";
        }

        /*
         * PT
         *
        */


        private void insertPT(string id)
        {
            try
            {
                channelptTableAdapter.InsertQuery(id, GlobalConstants.currentDevice, 0, 0, 0, 0, "");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }


        private void loadPT(string id)
        {
            try
            {
                string deviceCode = GlobalConstants.currentDevice;
                dtpt = channelptTableAdapter.GetDataBy(deviceCode, id);
                if (dtpt.Rows.Count == 0)
                {
                    insertPT(id);
                    setDefaultPT();
                }
                else
                {
                    DataRow row = dtpt.Rows[0];
                    loadDataToControls(row, numPTAlert1, numPTAlert2,
                        numPTStop1, numPTStop2, textBox12);
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }

        private void resetPT()
        {
            tbPTID.Text = "";
            numPTAlert1.Value = 0;
            numPTStop1.Value = 0;
            numPTAlert2.Value = 0;
            numPTStop2.Value = 0;
            textBox12.Text = "";
        }

        private void setDefaultPT()
        {
            numPTAlert1.Value = 0;
            numPTStop1.Value = 0;
            numPTAlert2.Value = 0;
            numPTStop2.Value = 0;
            textBox12.Text = "";
        }


        /*
         * FT
         *
        */


        private void insertFT(string id)
        {
            try
            {
                channelftTableAdapter.InsertQuery(id, GlobalConstants.currentDevice, 0, 0, 0, 0, "");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }


        private void loadFT(string id)
        {
            try
            {
                string deviceCode = GlobalConstants.currentDevice;
                dtft = channelftTableAdapter.GetDataBy(deviceCode, id);
                if (dtft.Rows.Count == 0)
                {
                    insertFT(id);
                    setDefaultFT();
                }
                else
                {
                    DataRow row = dtft.Rows[0];
                    loadDataToControls(row, numFTAlert1, numFTAlert2,
                        numFTStop1, numFTStop2, textBox13);
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }

        private void resetFT()
        {
            tbFTID.Text = "";
            numFTAlert1.Value = 0;
            numFTStop1.Value = 0;
            numFTAlert2.Value = 0;
            numFTStop2.Value = 0;
            textBox13.Text = "";
        }

        private void setDefaultFT()
        {
            numFTAlert1.Value = 0;
            numFTStop1.Value = 0;
            numFTAlert2.Value = 0;
            numFTStop2.Value = 0;
            textBox13.Text = "";
        }


        /*
         * RT
         *
        */


        private void insertRT(string id)
        {
            try
            {
                channelrtTableAdapter.InsertQuery(id, GlobalConstants.currentDevice, 0, 0, 0, 0, "");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }


        private void loadRT(string id)
        {
            try
            {
                string deviceCode = GlobalConstants.currentDevice;
                dtrt = channelrtTableAdapter.GetDataBy(deviceCode, id);
                if (dtrt.Rows.Count == 0)
                {
                    insertRT(id);
                    setDefaultRT();
                }
                else
                {
                    DataRow row = dtrt.Rows[0];
                    loadDataToControls(row, numRTAlert1, numRTAlert2,
                        numRTStop1, numRTStop2, textBox14);
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }

        private void resetRT()
        {
            tbRTID.Text = "";
            numRTAlert1.Value = 0;
            numRTStop1.Value = 0;
            numRTAlert2.Value = 0;
            numRTStop2.Value = 0;
            textBox14.Text = "";
        }

        private void setDefaultRT()
        {
            numRTAlert1.Value = 0;
            numRTStop1.Value = 0;
            numRTAlert2.Value = 0;
            numRTStop2.Value = 0;
            textBox14.Text = "";
        }

        private DataTable dtft;
        private DataTable dtte;
        private DataTable dtpt;
        private DataTable dtvt;
        private DataTable dtrt;


        private void btSaveFT_Click(object sender, EventArgs e)
        {
            if (!saveStrategy(tbFTID, numFTAlert1, numFTAlert2, numFTStop1, numFTStop2))
            {
                return;
            }
            channelftTableAdapter.UpdateQuery((int)numFTAlert1.Value, (int)numFTAlert2.Value,
                (int)numFTStop1.Value, (int)numFTStop2.Value, textBox13.Text,
                GlobalConstants.currentDevice, tbFTID.Text);

            UIUtil.getInstance().alert("保存成功。");
        }

        private void btSaveVT_Click(object sender, EventArgs e)
        {
            if (!saveStrategy(tbVTID, numVTAlert1, numVTAlert2, numVTStop1, numVTStop2))
            {
                return;
            }
            int i,j;
            int.TryParse(cbVTLength.Text, out i);
            int.TryParse(cbVTFrequency.Text, out j);
            channelvtTableAdapter.UpdateQuery(i, j, comboBoxTarget.Text,
                (int)numVTAlert1.Value, (int)numVTAlert2.Value,
                (int)numVTStop1.Value, (int)numVTStop2.Value, 
                tbTE.Text, GlobalConstants.currentDevice, tbVTID.Text);
            UIUtil.getInstance().alert("保存成功。");
        }

        private void btSavePT_Click(object sender, EventArgs e)
        {
            if (!saveStrategy(tbPTID, numPTAlert1, numPTAlert2, numPTStop1, numPTStop2))
            {
                return;
            }
            channelptTableAdapter.UpdateQuery((int)numPTAlert1.Value, (int)numPTAlert2.Value,
                (int)numPTStop1.Value, (int)numPTStop2.Value, textBox12.Text,
                GlobalConstants.currentDevice, tbPTID.Text);

            UIUtil.getInstance().alert("保存成功。");
        }

        private void btSaveRT_Click(object sender, EventArgs e)
        {
            if (!saveStrategy(tbRTID, numRTAlert1, numRTAlert2, numRTStop1, numRTStop2))
            {
                return;
            }
            channelrtTableAdapter.UpdateQuery((int)numRTAlert1.Value, (int)numRTAlert2.Value,
                (int)numRTStop1.Value, (int)numRTStop2.Value, textBox14.Text,
                GlobalConstants.currentDevice, tbRTID.Text);

            UIUtil.getInstance().alert("保存成功。");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (tbVTID.Text == "" || tbVTID.Text == null)
            {
                return;
            }
            loadVT(tbVTID.Text);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (tbTEID.Text == "" || tbTEID.Text == null)
            {
                return;
            }
            loadTE(tbTEID.Text);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (tbPTID.Text == "" || tbPTID.Text == null)
            {
                return;
            }
            loadPT(tbPTID.Text);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (tbFTID.Text == "" || tbFTID.Text == null)
            {
                return;
            }
            loadFT(tbFTID.Text);
        }


        private void button9_Click(object sender, EventArgs e)
        {
            if (tbRTID.Text == "" || tbRTID.Text == null)
            {
                return;
            }
            loadRT(tbRTID.Text);
        }

        private bool saveStrategy(TextBox tbID, NumericUpDown numAlert1, NumericUpDown numAlert2,
            NumericUpDown numStop1, NumericUpDown numStop2)
        {
            bool checkResult;

            if (tbID.Text == "" || tbID.Text == null)
            {
                UIUtil.getInstance().alert("请选择通道。");
                return false;
            }
            else
            {
                //checkResult = true;
            }

            if (numAlert1.Value >= numAlert2.Value)
            {
                UIUtil.getInstance().alert("请从小到大填写报警值。");
                checkResult = false;
            }
            else if (numStop1.Value >= numStop2.Value)
            {
                UIUtil.getInstance().alert("请从小到大填写停机值。");
                checkResult = false;
            }
            else if (numAlert1.Value <= numStop1.Value ||
                numAlert2.Value >= numStop2.Value)
            {
                UIUtil.getInstance().alert("报警高报值小于停机高报值/报警低报值大于停机低报值。");
                checkResult = false;
            }
            else 
            {
                checkResult = true;
            }
            
            return checkResult;
        }

        private void numericUpDown2_ValueChanged(object sender, EventArgs e)
        {

        }

        private void numVTStop_ValueChanged(object sender, EventArgs e)
        {

        }

        private void btSaveTE_Click(object sender, EventArgs e)
        {
            if (!saveStrategy(tbTEID, numTEAlert1, numTEAlert2, numTEStop1, numTEStop2))
            {
                return;
            }
            channelteTableAdapter.UpdateQuery((int)numTEAlert1.Value, (int)numTEAlert2.Value,
                (int)numTEStop1.Value, (int)numTEStop2.Value, tbTEComment.Text,
                GlobalConstants.currentDevice, tbTEID.Text);

            UIUtil.getInstance().alert("保存成功。");
        }

        private void loadDataToControls(DataRow row, NumericUpDown numAlert1, NumericUpDown numAlert2,
            NumericUpDown numStop1, NumericUpDown numStop2, TextBox tbComment)
        {
            decimal d1, d2, d3, d4;
            decimal.TryParse(row["alert1"].ToString(), out d1);
            numAlert1.Value = d1;
            decimal.TryParse(row["alert2"].ToString(), out d2);
            numAlert2.Value = d2;
            decimal.TryParse(row["stop1"].ToString(), out d3);
            numStop1.Value = d3;
            decimal.TryParse(row["stop2"].ToString(), out d4);
            numStop2.Value = d4;
            tbComment.Text = row["comment"].ToString();
        }
    }
}
